package com.g4.shoppingback.config;

import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices;
import org.springframework.security.oauth2.provider.token.DefaultTokenServices;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;

/**
 * Oauth2配置类
 */
@Configuration
@EnableAuthorizationServer //启用认证服务
@AllArgsConstructor //构造注入
public class Oauth2Config extends AuthorizationServerConfigurerAdapter {

    private BCryptPasswordEncoder passwordEncoder; //密码加密
    private AuthenticationManager authenticationManager; //认证管理对象
    private TokenStore tokenStore; //令牌商店
    private JwtAccessTokenConverter jwtAccessTokenConverter; //JWT令牌策略

    /**
     * 配置认证地址
     * @param security
     * @throws Exception
     */
    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
        security.tokenKeyAccess("permitAll()")  //认证地址"oauth/token"
                .checkTokenAccess("permitAll()")    //校验地址"/oauth/check_token"
                .allowFormAuthenticationForClients();   //允许表单提交
    }

    /**
     * 配置认证信息
     * @param clients
     * @throws Exception
     */
    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.inMemory().withClient("miShop-client")  //定义访问携带的请求头
                .secret(passwordEncoder.encode("123456")) //加密访问携带的认证信息
                .authorizedGrantTypes("password","refresh_token") //定义认证类型为密码方式和刷新令牌方式
                .scopes("all"); //要求满足所有条件
    }

    /**
     * 配置认证对象
     * @param endpoints
     * @throws Exception
     */
    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        endpoints.authenticationManager(authenticationManager)  //设置认证对象
                .tokenServices(tokenService())  //设置令牌服务对象
                .allowedTokenEndpointRequestMethods(HttpMethod.GET,HttpMethod.POST);    //设置认证访问的类型
    }

    /**
     * 配置令牌
     * @return 返回令牌服务对象
     */
    @Bean
    public AuthorizationServerTokenServices tokenService() {
        DefaultTokenServices tokenServices = new DefaultTokenServices(); //创建令牌服务
        tokenServices.setTokenStore(tokenStore); //设置令牌对象
        tokenServices.setTokenEnhancer(jwtAccessTokenConverter); //设置令牌为JWT
        tokenServices.setSupportRefreshToken(true); //设置令牌刷新
        tokenServices.setAccessTokenValiditySeconds(3600); //设置令牌有效期
        tokenServices.setRefreshTokenValiditySeconds(3600*24);  //设置刷新令牌时间
        return tokenServices;
    }
}
